package com.pk.platform.util;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.constant.Constant;

public class WebUtil implements HttpSessionListener {

	public void sessionCreated(HttpSessionEvent se)
	{
		
	}
	
	public void sessionDestroyed(HttpSessionEvent se)
	{
		OnlineCounter.remove(se.getSession());
	}
	
	
	/**
     * <p>
     * 说明:dwr request
     * </p>
     * @return
     */
    public static HttpServletRequest getDwrRequest()
    {
        WebContext web = WebContextFactory.get();
        HttpServletRequest request = web.getHttpServletRequest();
        return request;
    }
    
    /**
     * <p>
     * 说明:dwr response
     * </p>
     * @return
     */
    public static HttpServletResponse getDwrResponse()
    {
        WebContext web = WebContextFactory.get();
        HttpServletResponse response = web.getHttpServletResponse();
        return response;
    }
    
    /**
     * <p>
     * 说明:dwr session
     * </p>
     * @return
     */
    public static HttpSession getDwrSession()
    {
        HttpServletRequest request = getDwrRequest();
        return request.getSession();
    }
    
    /**
     * dwr user
     * @return
     */
    public static User getDwrUser()
    {
    	return (User)getDwrSession().getAttribute(Constant.USER);
    }
    
    
    /**
     * <p>
     * 说明:组装列表
     * </p>
     * @param str[0]:显示  str[1]:值
     * @return
     */
    public static List<SelectOption> constructOptions(String[][] str)
    {
        List<SelectOption> list = new ArrayList<SelectOption>();
        int len = str.length;
        if(len < 2)
        {
            return null;
        }
        int len2 = str[0].length;
        for(int j = 0; j < len2; j++)
        {
            SelectOption so = new SelectOption(str[0][j], str[1][j]);
            list.add(so);
        }
        return list;
    }
    
    
    /**
     * <p>
     * 说明:组装列表
     * </p>
     * @param
     * @return
     */
    public static List<SelectOption> constructOptions(String[] str)
    {
        List<SelectOption> list = new ArrayList<SelectOption>();
        for(String s : str)
        {
            SelectOption so = new SelectOption(s, s);
            list.add(so);
        }
        return list;
    }
    
    
    /**
     * 排序
     * @param size
     * @return
     */
    public static List<SelectOption> sortList(int size)
    {
    	List<SelectOption> list = new ArrayList<SelectOption>();
    	for(int i = 1; i <= size; i++)
    	{
    		SelectOption so = new SelectOption(i+"", i+"");
            list.add(so);
    	}
    	return list;
    }
	
}
